//
//  ViewController.m
//  UI01_UIWindow_UIView
//
//  Created by Scott on 16/5/3.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "CustomView.h"
#import "SimpleButton.h"

// 延展
@interface ViewController ()

@property (nonatomic, retain) CustomView *viewOfBlue;


@end

@implementation ViewController

- (void)dealloc {
    
    [_viewOfBlue release];
    [super dealloc];
}


#pragma mark - 知识点1: ViewController 生命周期
- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        NSLog(@"%s,%d", __FUNCTION__, __LINE__);
    }
    
    return self;
}

/** 
 * 如果重写VC的初始化方法, 通常重写此初始化方法.
 */
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    }
    return self;
}

/** 加载视图. */
- (void)loadView {
    
    [super loadView];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

/** 
 * 视图加载完毕.
 * 重写此方法的目的: 通常创建子控件在此方法中.
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    [self createSubViews];
    
}

/** 视图将要出现. */
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

/** 视图已经出现. */
- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

/** 当内存不足时, VC调用此方法. */
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
}

#pragma mark - 知识点2: UIView 
- (void)createSubViews {
    
    // 创建对象
    CustomView *viewOfRed = [[CustomView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    
    viewOfRed.backgroundColor = [UIColor redColor];
    
    viewOfRed.tag = 100;
    
    [self.view addSubview:viewOfRed];
    [viewOfRed release];
    
    // green View
    CustomView *viewOfGreen = [CustomView customViewWithFrame:CGRectMake(70, 70, 100, 100) color:[UIColor greenColor]];
    
    viewOfGreen.tag = 200;
    
    [self.view addSubview:viewOfGreen];
    
    
    // 创建blue 视图
    self.viewOfBlue = [CustomView customViewWithFrame:CGRectMake(20, 300, 300, 60) color:[UIColor blueColor]];
    
    [self.view addSubview:self.viewOfBlue];
    
    
    
    [self managerViewHierarchy];
    
    [self createButton];
}

#pragma mark - 知识点3: UIView的图层管理
- (void)managerViewHierarchy {
    
    // Core API: tag, viewOfTag, superView
    
    // 找到viewOfRed, viewOfGreen对象
    CustomView *viewOfRed = [self.view viewWithTag:100];
    
    CustomView *viewOfGreen = [self.view viewWithTag:200];
    
    NSLog(@"superView: %@", viewOfGreen.superview);

    // self.view 的子视图.
    NSLog(@"subViews:%@", self.view.subviews);
    
    // 把红色View放到绿色View上面
    [self.view bringSubviewToFront:viewOfRed];
    
    
    // 黄色View
    CustomView *viewOfYellow = [CustomView customViewWithFrame:CGRectMake(50, 50, 100, 100) color:[UIColor yellowColor]];
    
    [self.view insertSubview:viewOfYellow atIndex:1];
    
}

#pragma mark - 知识点4: UIButton(简单)
- (void)createButton {
    
    // 创建对象
    SimpleButton *buttonOfOK = [SimpleButton buttonWithType:UIButtonTypeSystem];
    
    buttonOfOK.frame = CGRectMake(60, 400, 100, 40);
    [self.view addSubview:buttonOfOK];
    
    buttonOfOK.backgroundColor = [UIColor yellowColor];
    
    // 设置Button标题.
    [buttonOfOK setTitle:@"OK" forState:UIControlStateNormal];
    
    [buttonOfOK setTitle:@"Down" forState:UIControlStateHighlighted];
    
    // 为button添加事件
    [buttonOfOK addTarget:self action:@selector(handleActon:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)handleActon:(SimpleButton *)button {
    
    // 更换背景颜色
    self.view.backgroundColor = self.viewOfBlue.backgroundColor;
    
}


#pragma mark - 知识点5: 程序启动流程
/** 详见 main.m 文件. */



@end
